<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

require_once(t3lib_extMgm::extPath('auxo') . 'core/class.tx_auxo_loader.php');
$loader = new tx_auxo_loader();

/**
 * @package auxo
 * @subpackage tests
 * @version $Id$
 */

class tx_auxo_mockFilter implements tx_auxo_FilterInterface {

	public $processed = false;
	public $initialized = false;
	public $counter = 0;
	
	public function initialize(tx_auxo_Configuration $configuration) {
		$this->initialized = true;
	}
	
	public function filter(tx_auxo_Request $request, tx_auxo_Response $response, tx_auxo_FilterChain $chain) {
		$this->processed = true;
		$this->counter++;
		$chain->invoke($request, $response);	
	}
}

/**	
 * The Filter Testcases
 *	
 * @package auxo
 * @subpackage core	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_Filter_testcase extends PHPUnit_Framework_testcase {
	/**
	 * Sets up a test enviroment of this testcase.
	 */
	public function setUp() {
	}
	
	/**
	 * Testcase: Checks wether a chain has been built and a filter is invoked
	 * 
	 * @return void
	 */
	public function testFilterChainWithOneFilterIsProcessed() {
		$mockConfiguration = new tx_auxo_Configuration(array());
		$mockEnvironment = new tx_auxo_ExtensionEnvironment('auxo');
		$mockRequest = new tx_auxo_Request($mockEnvironment);
		$mockResponse = new tx_auxo_Response($mockEnvironment);
		$chain = new tx_auxo_FilterChain($mockConfiguration, array($filter = new tx_auxo_mockFilter()));
		$this->assertType('tx_auxo_FilterChain', $chain);
		$chain->invoke($mockRequest, $mockResponse);
		$this->assertTrue($filter->processed, 'Filter not invoked');
		$this->assertTrue($filter->initialized, 'Filter not initialized');
	}

	/**
	 * Testcase: Checks wether a chain has been built and a list of filters is invoked
	 * 
	 * @return void
	 */
	public function testFilterChainWithAListOfFiltersIsProcessed() {
		$mockConfiguration = new tx_auxo_Configuration(array());
		$mockEnvironment = new tx_auxo_ExtensionEnvironment('auxo');
		$mockRequest = new tx_auxo_Request($mockEnvironment);
		$mockResponse = new tx_auxo_Response($mockEnvironment);
		for ($i = 0; $i < 3; $i++) {
			$filters[] = new tx_auxo_mockFilter();
		}
		$chain = new tx_auxo_FilterChain($mockConfiguration, $filters);
		$this->assertType('tx_auxo_FilterChain', $chain);
		$chain->invoke($mockRequest, $mockResponse);
		
		foreach ($filters as $filter) {
			$this->assertTrue($filter->processed, 'Filter not invoked');
			$this->assertTrue($filter->initialized, 'Filter not initialized');
			$this->assertEquals(1, $filter->counter);
		}
	}
	
	/**
	 * Tears down a created test enviroment of this testcase.
	 */
	public function tearDown() {
	}
}